﻿using MvcPaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;

namespace zichan.BLL
{
   public class FieldsService
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string AddFields(AsseteField model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try {
                    if (db.AsseteFields.Count(o => o.Name == model.Name&&o.AsseteClassId==model.AsseteClassId) > 0)
                        return model.Name + "已存在";
                    db.AsseteFields.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch(Exception ex)
                {
                    return "添加失败，请重新添加";
                }
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string EditFields(AsseteField model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.AsseteFields.Count(o => o.Id != model.Id && o.Name == model.Name) > 0)
                    return model.Name + "已存在";
                db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();

                return string.Empty;
            }
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string RemoveFields(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                AsseteField m = db.AsseteFields.FirstOrDefault(o => o.Id == id);
                if (m != null)
                {
                    //if (db.ConsumateInfos.Count(o => o.FieldId == id) > 0)
                    //{
                    //    return m.Name + "不能删除，已在使用中";
                    //}
                    db.AsseteFields.Remove(m);
                    db.SaveChanges();
                    ClassLogService.AddLog("资产分类", "删除" + "\""+AsseteClassService.GetAsseteClass(m.AsseteClassId).Name +",字段:"+ m.Name + "\"");
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static AsseteField GetField(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                AsseteField model= db.AsseteFields.FirstOrDefault(o => o.Id == Id);
                model.AssetClassName = AsseteClassService.GetAsseteClass(model.AsseteClassId).Name;
                return model;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public static IPagedList<AsseteField> GetFieldsList(string name,int assetClassId, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<AsseteField> w = db.AsseteFields.Where(o=>o.AsseteClassId==assetClassId);
                if (!string.IsNullOrEmpty(name))
                    w = w.Where(o => o.Name.Contains(name));

               List<AsseteField> list = w.OrderBy(o => o.Sort).ThenBy(o => o.Id).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                foreach (AsseteField c in list)
                {
                    c.UseCount = db.Assets.Count(o => o.AssetClassId== assetClassId);
                    
                }
                return list.OrderBy(o => o.Sort).ThenBy(o => o.Id).ToPagedList(pageIndex - 1, pageSize, w.Count());
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="nameList">输入的名称集合</param>
        /// <param name="existName">重复的名称</param>
        /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
        /// <returns></returns>
        public static string AddBatchFields(List<string> nameList,int assetClassId, out List<string> existName, out List<string> nameListForLog)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    int maxSort = db.AsseteFields.Max(o => o.Sort);
                    List<AsseteField> classList = new List<AsseteField>();
                    foreach (string n in nameList)
                    {
                        if (db.AsseteFields.Count(o => o.Name == n&&o.AsseteClassId==assetClassId) > 0 || classList.Count(o => o.Name == n) > 0)
                        {
                            existName.Add(n);
                            continue;
                        }
                        nameListForLog.Add(n);
                        AsseteField model = new AsseteField();
                        maxSort += 1;
                        model.Name = n;
                        model.Sort = maxSort;
                        model.AsseteClassId = assetClassId;
                        classList.Add(model);
                    }
                    if (classList.Count() > 0)
                    {
                        db.AsseteFields.AddRange(classList);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.AsseteFields.Max(o => o.Sort);
            }
        }
        public static List<AsseteField> List(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.AsseteFields.Where(o => o.AsseteClassId == id).ToList();
            }
        }
      
    }
}
